package 二叉树;

/**
 * https://leetcode-cn.com/problems/merge-two-binary-trees/submissions/
 * 递归一下，如果树是空的返回另一颗就好，不空的话把值相加，然后继续递归左子树和右子树进行判断
 */
public class 合并二叉树 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null || root2 == null){
            return root1 == null ? root2 : root1;
        }
        return cur(root1 , root2);
    }
    public TreeNode cur(TreeNode root1 , TreeNode root2){
        if (root1 == null || root2 == null){
            return root1 == null ? root2 : root1;
        }
        root1.val += root2.val;
        root1.left = cur(root1.left , root2.left);
        root1.right = cur(root1.right , root2.right);
        return root1;
    }
}
